﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Company.Database;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using System.Windows;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    public class InitializeViewModel : ObservableObject
    {
        private object? content;
        /// <summary>
        /// 页面
        /// </summary>
        public object? Content
        {
            get => content;
            set => SetProperty(ref content, value);//属性值变化时通知
        }

        private Visibility? showButton = Visibility.Collapsed;
        /// <summary>
        /// 是否显示按钮
        /// </summary>
        public Visibility? ShowButton
        {
            get => showButton;
            set => SetProperty(ref showButton, value);//属性值变化时通知
        }

        public ICommand? LoadedCommand { get; }

        public ICommand? InitializeCommand { get;  }
        private IUserRepository UserRepository { get; }
        private ISession Session;
        private IRoomManager RoomManager { get; }
       
        private ICollectManager CollectManager { get; }
        private IHardwareManager HardwareManager;
        public InitializeViewModel(
           
            ICollectManager collectManager,
            IRoomManager roomManager,
            IHardwareManager hardwareManager,
            ISession session,
            IUserRepository userRepository)
        {
            
            CollectManager = collectManager;
            RoomManager = roomManager;
            HardwareManager = hardwareManager;
            Session = session;
            UserRepository = userRepository;
            LoadedCommand = new AsyncRelayCommand(OnLoadedCommand);//加载命令   
            InitializeCommand = new AsyncRelayCommand(OnInitializeCommand);//初始化命令
            ShowButton = Visibility.Collapsed;//初始不显示按钮
            Content = "正在初始化数据库，请稍后...";//初始页面内容

        }

        private async Task OnLoadedCommand()
        {
            // 显示数据库初始化消息
            Content = "正在初始化数据库，请稍后...";
            // 模拟数据库初始化过程的延迟
            await Task.Delay(1000);
            var user = UserRepository.search("guest");
            if (user == null)
            {
                user = new Database.Entities.UserEntity
                {
                    UserName = "guest",
                    Password = "123",
                    Role = 1,

                };
                var success = UserRepository.Insert(user);
                if (success)
                {
                   Session.Add(user);
                }
                else
                {
                    Session.Add(user);
                }
                var admin = UserRepository.search("admin");
                if (admin == null)
                {
                    admin = new Database.Entities.UserEntity
                    {
                        UserName = "admin",
                        Password = "admin",
                        Role = 1,
                    };
                    UserRepository.Insert(admin);
                }
               
            }
            Content = "房间初始化";
            await RoomManager.Initialize();
            Content = "数据库初始化完成，正在连接PLC";
            await Task.Delay(1000);//模拟硬件初始化过程
            var result =  await HardwareManager.InitalizeAsync();//初始化硬件
      
            if (result.Success)
            {
                //开始运行
                CollectManager.Start();
                //
                WeakReferenceMessenger.Default.Send(new InitializeSuccessMessage(result.Message));//发送消息通知主界面切换到控制页面
            }
            else
            {
                Content = $"硬件初始化失败，{result.Message},请检查硬件连接后点击按钮重新初始化";
                ShowButton = Visibility.Visible;//显示按钮
            }

        }

        private async Task OnInitializeCommand()
        {
            await Task.Delay(1000);//模拟点击按钮后的操作
            Content = "正在重新连接PLC，请稍后...";
            ShowButton = Visibility.Collapsed;//隐藏按钮
            var result = await HardwareManager.InitalizeAsync();//重新初始化硬件
            if (result.Success)
            {
                //开始运行
                CollectManager.Start();
                //跳转首页 
                WeakReferenceMessenger.Default.Send(new InitializeSuccessMessage(result.Message));//发送消息通知主界面切换到控制页面
            }
            else
            {
                Content = $"硬件初始化失败，{result.Message},请检查硬件连接后点击按钮重新初始化";
                ShowButton = Visibility.Visible;//显示按钮
            }
        }    
    }
}
